package com.example.ymm.modules1.controller.ms;

import com.alibaba.fastjson.JSONObject;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.ms.MsOrderItemView;
import com.example.ymm.modules1.query.ms.MsOrderItemViewQueryCriteria;
import com.example.ymm.modules1.service.dto.ms.ListenerOutOrderItemDto;
import com.example.ymm.modules1.service.dto.ms.ListenerStockItemDto;
import com.example.ymm.modules1.service.ms.MsOrderItemViewService;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.EmailUtil;
import com.example.ymm.util.UtilValidate;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/ms/api")
@Tag(name =  "ms api 管理")
@Slf4j
public class MsApiController extends BaseController {

    @Autowired
    MsOrderItemViewService msOrderItemViewService;

    @Schema( name =  "时俱佳订单信息接口")
    @RequestMapping(value = "/orderItemList", method = RequestMethod.POST)
    public CommonResult orderItemList(HttpServletRequest request, MsOrderItemViewQueryCriteria criteria, @Valid YmPageDto ymPage) {
        try {
            String pcPassword = criteria.getPcCertified();
            System.out.println("--pc_certified:"+pcPassword);
            if(UtilValidate.isEmpty(pcPassword)){
                return CommonResult.error("非法请求！");
            }
            if(!pcPassword.equals("Kp@7Gm$2Rt%9Qw&5Jh#3Nz^8Lf*4Ds!6Xc$1Vb&7Mn")){
                return CommonResult.error("非法请求");
            }
            TableList<MsOrderItemView> list = msOrderItemViewService.list(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "监听 领料出库动作")
    @RequestMapping(value = "/listenerAddOutOrder", method = RequestMethod.POST)
    public CommonResult listenerAddOutOrder(HttpServletRequest request, @RequestBody List<ListenerOutOrderItemDto> dtos) {
        try {
            //Kp8@Qw$7Rt%Yh^9Zu*Mj#3Xc
            request.setCharacterEncoding("UTF-8");
            String pcCertified = request.getHeader("pcCertified");
            System.out.println("--pc_certified:"+pcCertified);
            if(UtilValidate.isEmpty(pcCertified)){
//                return CommonResult.error("非法请求！");
                log.info("--非法请求1:"+pcCertified);
            }
            if(UtilValidate.isNotEmpty(pcCertified)){
                if(!pcCertified.equals("Kp8@Qw$7Rt%Yh^9Zu*Mj#3Xc")){
//                return CommonResult.error("非法请求");
                    log.info("--非法请求2:"+pcCertified);
                }
            }

            msOrderItemViewService.listenerAddOutOrder(dtos);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            EmailUtil.sendEmail("1055162826@qq.com","监听 领料出库动作异常:"+ DateKit.getStringNowTime(),e.getMessage());
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "监听 选中的库存数据")
    @RequestMapping(value = "/listenerClickStockItem", method = RequestMethod.POST)
    public CommonResult listenerClickStockItem(HttpServletRequest request, @RequestBody List<ListenerStockItemDto> items) {
        try {
            request.setCharacterEncoding("UTF-8");
            String pcCertified = request.getHeader("pcCertified");
            System.out.println("--pc_certified:"+pcCertified);
            if(UtilValidate.isEmpty(pcCertified)){
//                return CommonResult.error("非法请求！");
                log.info("--非法请求1:"+pcCertified);
            }
            if(UtilValidate.isNotEmpty(pcCertified)){
                if(!pcCertified.equals("Kp8@Qw$7Rt%Yh^9Zu*Mj#3Xc")){
//                return CommonResult.error("非法请求");
                    log.info("--非法请求2:"+pcCertified);
                }
            }
            log.info("-- 库存-items:"+items);

            msOrderItemViewService.listenerClickStockItem(items);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            EmailUtil.sendEmail("1055162826@qq.com","监听 选中的库存数据:"+ DateKit.getStringNowTime(),e.getMessage());
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "监听 选中的库存数据")
    @RequestMapping(value = "/listenerClickStockItem2", method = RequestMethod.POST)
    public CommonResult listenerClickStockItem2(HttpServletRequest request, @RequestBody JSONObject query) {
        try {

            return CommonResult.success(query);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


}
